<?php
namespace Content\Controller;
use Common\Controller\AdminbaseController;
class TypeController extends AdminbaseController 
{   
    protected $typeObj;
	public function _initialize()
	{
        parent::_initialize();
        $this->typeObj = D("Content/Type");
	}

	public function index()
	{
			$this->lists();
	}

	public function lists()
	{
        $typeTree = $this->tree(0,0);
        $this->assign("list",$typeTree);
        $this->display('list');
	}

    public function tree($parentid=0,$level=0,$html='|── ')
    {
        static $tree = array();
        $typeList = $this->getTypeList($parentid);
        if (!empty($typeList)) 
        {
            foreach($typeList as $type)
            {
                $type['sort'] = $level;
                $html=str_repeat($html,$level);
                $type['html'] = $html;
                $tree[] = $type;
                $parentId = $type['id'];
                $this->tree($parentId,$level+1);
            }
        }
        return $tree;
    }
    public function getTypeList($parentId)
    {
        $typeList = $this->typeObj->where(array("parentid" => $parentId))->order(array("id" => "ASC"))->select();
        return $typeList;
    }

    
    public function add()
    {
        $parentId = $_GET['parentid'];
        $this->assign('parentId',$parentId);
    	$this->display();
    }
    public function edit($id=0)
    {
    	$typeObj=null;
    	if ($id>0) 
    	{
            $typeObj=$this->typeObj->relation(true)->find($id);
    	    $this->assign("type",$typeObj);
        }
    	$this->display();
    }
    public function save()
    {
        $id = I('post.inputId','','htmlspecialchars');
        $parentId = I('post.inputParent','0','htmlspecialchars');
        $name = I('post.inputName','','htmlspecialchars');
        $url = I('post.inputUrl','','htmlspecialchars');
        $templateList = I('post.inputTemplatelist','','htmlspecialchars');
        $template = I('post.inputTemplate','','htmlspecialchars');
        $status = I('post.inputStatus','','htmlspecialchars');
        $data = array();
        $data['parentid']=$parentId;
        $data['name']  = $name;
        $data['templatelist'] = $templateList;
        $data['template'] = $template;
        $data['status'] = $status;
        $data["url"]=$url;
        $redirect["rtype"]=I('post.inputRedirectType','list','htmlspecialchars');
        if($id>0)
        {
            $redirect["rid"]=I('post.inputRedirectId','','htmlspecialchars');
            $data['redirect']=serialize($redirect);
            $editResult = $this->typeObj->where("id=%d",$id)->save($data);
            $arr = array("status"=>'1',"message"=>"编辑栏目分类成功","url"=>"index");
        }
        else
        {
            $addResult =  $this->typeObj->add($data);
            if($addResult) {
                $redirect["rid"]=$addResult;
                $rdata['redirect']=serialize($redirect);
                $editResult = $this->typeObj->where("id=%d",$addResult)->save($rdata);
                $arr = array("status"=>'1',"message"=>"添加栏目分类成功","url"=>"index");
            }else{
                $arr = array("status"=>'0',"message"=>"添加栏目分类失败");
            }
        }
        echo json_encode($arr);
    }
	/*排序更新*/
    public function listorder()
    {
       $ids = I('post.ids','-1','htmlspecialchars');
       $listorders = I('post.listorders','','htmlspecialchars');  
       parent::_listorder($this->typeObj,$ids,$listorders);
    }
    /*单个删除 id*/
    public function delete($id)
    {
        parent::_delete($this->typeObj,$id);
    }

    /* 批量删除 ids*/
    public function checkDeletes($ids)
    {
        parent::_checkDeletes($this->typeObj,$ids);
    }
    /* 单个修改当前状态status 0 1 -1*/
    public function changeStatus($id,$status)
    {
        parent::_changeStatus($this->typeObj,$id,$status);
    }
    /*批量 修改当前状态status 0 1 -1*/
    public function checkChanges($status,$ids)
    {
        parent::_checkChanges($this->typeObj,$status,$ids);
    }
}
?>